package br.ufc.engsoft.smm;

import java.util.ArrayList;

import br.ufc.engsoft.smm.banco.JogadorWSDL;
import br.ufc.engsoft.smm.banco.ConexaoWebService;
import br.ufc.engsoft.smm.jogo.JogoController;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;


public class MenuPrincipal extends Activity{
	public MediaPlayer mp;
    TextView numP;
	public class listener implements DialogInterface.OnClickListener{
		Activity atividade;
		public listener(Activity Atividade){
			atividade=Atividade;
		}
		public void onClick(DialogInterface dialog, int which) {
			Intent i = new Intent(atividade,JogoController.class);
			i.putExtra("dificuldade", which);
			mp.stop();
			startActivity(i);	
		}
	}
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int qtd = ConexaoWebService.qtdPerguntas();
        setContentView(R.layout.menuprincipal);
        numP = (TextView) findViewById(R.id.numPerguntas);
        numP.setText("Total de Perguntas no Banco: "+ qtd);
        mp = MediaPlayer.create(getBaseContext(), R.raw.tema01);
        mp.setLooping(true);
        mp.start();
	}
	public void jogo1Pessoa(View v) {
		final CharSequence[] items = {"Facil", "Medio","Dificil","Muito Dificil"};
    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	builder.setTitle("Dificuldade:");
    	listener listen = new listener(this);
    	builder.setItems(items, listen);
    	AlertDialog alert = builder.create();
    	alert.show();
    }
	public void verRanking(View v) {
		ArrayList<JogadorWSDL> ranking = ConexaoWebService.recebeRanking();
		String resultado = "";
		for(int i=0; i<ranking.size();i++){
			resultado += (i+1) + " - " + ranking.get(i).getProperty(0) + " - " + ranking.get(i).getProperty(1) + "Pts\n";
		}
		AlertDialog alertDialog;
		alertDialog = new AlertDialog.Builder(this).create();
		alertDialog.setTitle("Ranking - TOP 5");
		alertDialog.setMessage(resultado);
		alertDialog.setButton("Ok", new DialogInterface.OnClickListener() {
    		public void onClick(DialogInterface dialog, int item) {
    			dialog.dismiss();
    		}
    	});
		alertDialog.show();
	}
	public void Sair(View v) {
		if(mp.isLooping()){
			mp.stop();
			mp.release();
		}
		MediaPlayer tchau;
		tchau = MediaPlayer.create(getApplicationContext(), R.raw.tchau);
		tchau.setLooping(false);
		tchau.start();
		while(tchau.isPlaying()){}
		tchau.release();
    	finish();
    }
}
